package media.opensync;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

public class ViewMarked extends Activity {

	public AdaptorMarked adaptor = new AdaptorMarked(this);;
	private ListView listView;
	public static int itemPos;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.marked);
		
		SortedSet<String> filesMarked = DirList.filesToSend;
		adaptor.addFiles(filesMarked);
		listView = (ListView)findViewById(R.id.marked);
		listView.setAdapter(adaptor);
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> av, View v, int pos, long id) {
				dialog(pos);
			}
		});
	}
	
	public void dialog(int pos){
		itemPos = pos;
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Are you sure you want to unmark this file?")
		.setCancelable(false)
		.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				adaptor.delItem(itemPos);
			}
		})
		.setNegativeButton("No", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				dialog.cancel();
			}
		});
		AlertDialog alert = builder.create();
		alert.show ();
	}
	
	public boolean onCreateOptionsMenu(Menu menu){
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.layout.menu2,menu);
		return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item){
		switch (item.getItemId()){
		case R.id.back:
			this.finish();
			return true;
		case R.id.send:
			int nrFiles = DirList.filesToSend.size();
			try{
				Socket socket = new Socket(DirList.SERVERIP, DirList.SERVERPORT);
				
				DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
				//sending password after encrypting it:
				MDCalculator mdcalc = new MDCalculator(TheOpenSyncActivity.password);
				String pass = mdcalc.getMD();
				dos.writeUTF(pass);
				//sending number of files:
				dos.writeInt(nrFiles);
				//sending each file:
				Iterator<String> it = DirList.filesToSend.iterator();
				while(it.hasNext()){
					String path = it.next();
					File f = new File(path);
					int idx = path.lastIndexOf("/");
					int n = path.length();
					String fileName = path.substring(idx + 1, n);
					sendFile(socket, dos, f, fileName);
				}
				DirList.filesToSend = new TreeSet<String>();
				dos.close();
			} catch (UnknownHostException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return true;
		}
		return false;
	}
	
	public void  sendFile(Socket socket, DataOutputStream dos, File f, String fileName) {

		try {
			FileInputStream fis = new FileInputStream(f);
			int length = (int) f.length();
			byte [] byteArray = new byte[length];
			DataInputStream dis = new DataInputStream (new BufferedInputStream(fis));
			
			dis.readFully(byteArray);
			
			dos.writeInt(length);
			dos.writeUTF(fileName);			
			dos.write(byteArray);
			
			this.finish();
		} catch (IOException e) {
			Toast.makeText(ViewMarked.this, "ERROR! "+e.getMessage(), Toast.LENGTH_SHORT).show();
			e.printStackTrace();
		}
	}
}